﻿using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {

        [CtxMenuItem("Renderer/Shapes/Remove/Shape",
           "Removes a blend-shapes from a mesh.", ContextFlag.Blendshape, 001)]
        public static void RendererShapesRemoveSingle()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var mesh = context.GetSharedMesh().Clone() as Mesh;

                mesh.RemoveBlendshape(SkinnContext.ContextShape0);

                SkinnContext.UndoRecord(context, "Remove Shape");
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Shapes/Remove/Data/Vertices/Shape",
          "Removes a blend-shapes vertices from a mesh.", ContextFlag.Blendshape, 001)]
        public static void RendererShapesRemoveDataSingleVertex()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var mesh = context.GetSharedMesh().Clone() as Mesh;

                mesh.RemoveBlendshapeData(SkinnContext.ContextShape0, true, false, false);

                SkinnContext.UndoRecord(context, "Remove Shape Vertices");
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Shapes/Remove/Data/Vertices/All",
        "Removes all blend-shapes vertices from a mesh.", 001)]
        public static void RendererShapesRemoveDataVertex()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var mesh = context.GetSharedMesh().Clone() as Mesh;

                mesh.RemoveBlendshapeData(true, false, false);

                SkinnContext.UndoRecord(context, "Remove All Shapes Vertices");
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Shapes/Remove/Data/Normals/Shape",
       "Removes a blend-shapes normals from a mesh.", ContextFlag.Blendshape, 001)]
        public static void RendererShapesRemoveDataSingleNormals()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var mesh = context.GetSharedMesh().Clone() as Mesh;

                mesh.RemoveBlendshapeData(SkinnContext.ContextShape0, false, true, false);

                SkinnContext.UndoRecord(context, "Remove Shape Normals");
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Shapes/Remove/Data/Normals/All",
       "Removes all blend-shapes normals from a mesh.", 001)]
        public static void RendererShapesRemoveDataNormals()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var mesh = context.GetSharedMesh().Clone() as Mesh;

                mesh.RemoveBlendshapeData(false, true, false);

                SkinnContext.UndoRecord(context, "Remove All Shapes Normals");
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Shapes/Remove/Data/Tangents/Shape",
      "Removes a blend-shapes tangents from a mesh.", ContextFlag.Blendshape, 001)]
        public static void RendererShapesRemoveDataSingleTangents()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var mesh = context.GetSharedMesh().Clone() as Mesh;

                mesh.RemoveBlendshapeData(SkinnContext.ContextShape0, false, true, false);

                SkinnContext.UndoRecord(context, "Remove Shape Tangents");
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Shapes/Remove/Data/Tangents/All",
      "Removes all blend-shapes tangents from a mesh.", 001)]
        public static void RendererShapesRemoveDataTangents()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var mesh = context.GetSharedMesh().Clone() as Mesh;

                mesh.RemoveBlendshapeData(false, false, true);

                SkinnContext.UndoRecord(context, "Remove All Shapes Tangents");
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Shapes/Remove/Unused",
           "Remove all unused blend-shapes from a mesh.", 001)]
        public static void RendererShapesRemoveUnused()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var mesh = context.GetSharedMesh().Clone() as Mesh;

                mesh.ClearUnusedBlendshapes();

                SkinnContext.UndoRecord(context, "Remove Unused Shapes");
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Shapes/Remove/All",
            "Remove all blend-shapes from a mesh.", 001)]
        public static void RendererShapesRemoveAll()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var mesh = context.GetSharedMesh().Clone() as Mesh;
                mesh.ClearBlendShapes();

                SkinnContext.UndoRecord(context, "Remove All Shapes");
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("SkinnedMeshRenderer/Shapes/Merge Duplicate Shapes",
           "Merge Blend-shapes that have the same (Support for Poser Exports).", 001)]
        public static void SkinnedMeshRendererShapesMergeDuplicateShapes()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<SkinnedMeshRenderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                SkinnContext.UndoRecord(context, "Merge Duplicate Shapes");

                context.ResetBlendshapes();
                var mesh = context.sharedMesh.Clone() as Mesh;
                mesh.CombineDuplicateShapes();

                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("SkinnedMeshRenderer/Shapes/Zero Values",
          "Set all blend-shape float values in the component to zero.", 001)]
        public static void SkinnedMeshRendererShapesZeroValues()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<SkinnedMeshRenderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                SkinnContext.UndoRecord(context, "Zero Blend-shape Values");
                var blendShapeCount = context.sharedMesh.blendShapeCount;
                for (int i = 0; i < blendShapeCount; i++) context.SetBlendShapeWeight(i, 0f);
            }

            SkinnContext.Finished();
        }

    }
}